<?php
include '../sys_application.php';
include 'func/customerInfo.php';
include 'func/func.php';

$sysApps = new sysApps();
$sysApps->DBconnect();

$realParent = $_GET['child'];
echo 'parent = '.$realParent;
$sql = "SELECT mu.id, mu.email, mu.url_name, mu.phone_no, mu.user_MGM_id, mu.fullname, mu.identity_no, mu.address1, mu.address2, mu.mobile_no, us.start_month, us.end_month
		FROM mgm_users mu
		LEFT JOIN user_subscription us
		ON mu.id = us.user_id
		WHERE mu.sponsor_id = '$realParent'
		AND (us.end_month = (SELECT MAX(us1.end_month)
							FROM user_subscription us1
							WHERE us1.user_id = mu.id)
			OR us.end_month IS NULL )
		GROUP BY mu.id
		ORDER BY mu.id
		LIMIT 2";


$result = mysql_query($sql);

$sql2 = "SELECT mu.id, mu.email, mu.url_name, mu.phone_no, mu.user_MGM_id, mu.fullname, mu.identity_no, mu.address1, mu.address2, mu.mobile_no, us.start_month, us.end_month
		FROM mgm_users mu
		LEFT JOIN user_subscription us
		ON mu.id = us.user_id
		WHERE mu.user_MGM_id = '$realParent'
		AND (us.end_month = (SELECT MAX(us1.end_month)
							FROM user_subscription us1
							WHERE us1.user_id = mu.id)
			OR us.end_month IS NULL )";

$result2 = mysql_query($sql2);
$numrowcheck = mysql_num_rows($result);
$numrowcheck2 = mysql_num_rows($result2);
//echo "numrowcheck = ".$numrowcheck." || numrowcheck = ".$numrowcheck." || realParent = ".$realParent;
$allInfo = new retreaveInfo(0, 0, " ");
$allchild = array();
$totalChild = 0;
$listAllInfo = array();
while($row = mysql_fetch_array($result))
{	
	$retreaveInfo = new retreaveInfo(0, 0, " ");
	$allInfo = calculateIncome($row['user_MGM_id'], $retreaveInfo);
	$statusActive = checkStatus($row['start_month'],$row['end_month']);
	$obj=new CustomerInfo($row['id'], $row['email'], $row['url_name'], $row['phone_no'], $row['user_MGM_id'], $row['fullname'], $row['identity_no'], $row['address1'], $row['address2'], $row['mobile_no'], intval($allInfo->getTotalChild()), "", $statusActive);
	$allchild[] = $obj;
	
	if(trim($allInfo->getTrees()) == "" )
	{
		$dummy = "<li><a href='childIncome.php?child=".$row['user_MGM_id']."' ".childNodeStyle($statusActive).">".$row['url_name']."</a></li>";
		$totalChild = $totalChild + 1;
	}
	else {
		$dummy = "<li><a href='childIncome.php?child=".$row['user_MGM_id']."' ".childNodeStyle($statusActive).">".$row['url_name']."</a><ul>".$allInfo->getTrees()."</ul></li>";
		$totalChild = $totalChild + intval($allInfo->getTotalChild());
	}
	$allInfo->setTrees($dummy);
	$listAllInfo[] = $allInfo;
}

$userProfileDB = mysql_fetch_array($result2);
$statusParent = checkStatus($userProfileDB['start_month'],$userProfileDB['end_month']);
$userProfile=new CustomerInfo($userProfileDB['id'], $userProfileDB['email'], $userProfileDB['url_name'], $userProfileDB['phone_no'], $userProfileDB['user_MGM_id'], $userProfileDB['fullname'], $userProfileDB['identity_no'], $userProfileDB['address1'], $userProfileDB['address2'], $userProfileDB['mobile_no'], intval($allInfo->getTotalChild()), "", $statusParent);

mysqli_close($con);
?>



    <div id="childInfo">Keyssss<br/>
    	<div class="tree">
    		<ul>
    			<li><a href="#" <?php echo childNodeStyle($userProfile->getStatus()); ?> ><?php echo $userProfile->getUserName();?></a>
    				<?php 
    					if(!empty($listAllInfo))
    					{
	    					echo "<ul>";
					    	
					    		$count = 0;
					    		foreach ($listAllInfo as $info) 
								{
									echo $info->getTrees();
								}
							echo "</ul>";
    					}
					?>
				</li>
    		</ul>
    	</div>
    </div>

